﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
<link href="~/css/site.css" rel="stylesheet" />
<link href="~/layui/css/layui.css" rel="stylesheet" />
<div class="layui-container" style="padding:20px">
    <div class="layui-row">
        <div class="layui-col-md12">
            <h2>开票管理列表</h2>
        </div>
    </div>

    <div class="layui-row">
        <div class="layui-col-md12">
            <div class="layui-btn-group">
                <button class="layui-btn layui-btn-normal" id="addOpenFare">
                    <i class="layui-icon layui-icon-add-1"></i> 新增开票
                </button>
                <button class="layui-btn layui-btn-primary" id="refreshList">
                    <i class="layui-icon layui-icon-refresh"></i> 刷新
                </button>

            </div>
        </div>
    </div>
    
    <div class="layui-row">
        <div class="layui-col-md12">
            <table class="layui-table">
                <thead>
                    <tr>
                        <th>开票Id</th>
                        <th>开票单号</th>
                        <th>开票主题</th>
                        <th>发票类型</th>
                        <th>开票日期</th>
                        <th>收票方</th>
                        <th>开票金额</th>
                        <th>发票号</th>
                        <th>备注</th>
                        <td>操作列</td>
                    </tr>
                </thead>
                <tbody id="tbody"></tbody>
            </table>
            <div id="demo-laypage-all"></div>
        </div>
    </div>
</div>

<!-- 先引入jQuery，再引入其他脚本 -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="~/js/site.js"></script>
<script src="~/layui/layui.js"></script>

<script>
    var index = 1;
    var size = 15;
    var totalCount = 0;
    var laypageInited = false;

    $(function () {
        // 绑定按钮事件
        $('#addOpenFare').click(function(){
            window.location.href = '/OpenFare/AddOpenFare';
        });
        
        $('#refreshList').click(function(){
            Index();
        });

        // 首次加载
        Index();
    })
    
    //显示
    function Index() {
        var pageIndexParam = Math.max(1, parseInt(index, 10)); // 后端 index 从1开始
        $.ajax({
            url: 'http://localhost:5054/api/OpenFare/GetOpenFare?index=' + pageIndexParam + '&size=' + size + '&_=' + new Date().getTime(),
            dataType: 'json',
            type: 'get',
            contentType: 'application/json',
            cache: false,
            async: true,
            success: function(res) {
                var str = '';
                // 后端API：{ totalCount, totalPCount, data: [...] }
                var rows = (res && res.data) ? res.data : [];
                var newTotal = (res && (res.totalCount || res.TotalCount)) ? (res.totalCount || res.TotalCount) : 0;
                var previousTotal = totalCount;
                totalCount = newTotal;

                if (rows.length > 0) {
                    $(rows).each(function (i) {
                        str += `<tr>
                                <td>${((index - 1) * size) + i + 1}</td>
                                <td>${this.openFareCode || ''}</td>
                                <td>${this.openFareTheme || ''}</td>
                                <td>${this.invoiceTypeName || ''}</td>
                                <td>${this.openFareTime || ''}</td>
                                <td>${this.collectTickets || ''}</td>
                                <td>${this.openFareMoney || ''}</td>
                                <td>${this.invoiceTypeCode || ''}</td>
                                <td>${this.openFareRemark || ''}</td>
                                <td>
                                    <button type="button" class="layui-btn" onclick="Del(${this.openFareId})">删除</button>
                                    <button type="button" class="layui-btn" onclick="Update(${this.openFareId})">修改</button>
                                    <button type="button" class="layui-btn" onclick="XiangQing(${this.openFareId})">详情</button>
                                </td>
                            </tr>`;
                    });
                } else {
                    str = '<tr><td colspan="10" style="text-align:center;color:#999;">暂无数据</td></tr>';
                }
                $('#tbody').html(str);

                // 首次或总数变化时，重绘分页
                if (!laypageInited || previousTotal !== totalCount) {
                    renderPager();
                    laypageInited = true;
                }
            },
            error: function(xhr, status, error) {
                console.error('加载数据失败:', error);
                $('#tbody').html('<tr><td colspan="10" style="text-align:center;color:#f00;">加载数据失败，请检查网络连接</td></tr>');
            }
        });
    }

    function renderPager() {
        layui.use(['laypage'], function(){
            var laypage = layui.laypage;
            laypage.render({
                elem: 'demo-laypage-all',
                count: totalCount,
                limit: size,
                curr: index,
                limits: [10, 15, 20, 50],
                layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
                jump: function(obj, first){
                    index = obj.curr;
                    size = obj.limit;
                    if (!first) {
                        Index();
                    }
                }
            });
        });
    }
 
    //删除
    function Del(openFareId) {
        if(confirm("确定删除吗？"))
        {
            $.ajax({
                url: 'http://localhost:5054/api/OpenFare/DelOpenFare?OpenFareId=' + openFareId,
                dataType:'json',
                type:'get',
                contentType: 'application/json',
                success: res => { 
                    if (res&&(res.code === 200)) {
                        layer.msg("开票管理删除成功！");
                        Index();
                    }
                    else {
                        layer.msg("开票管理删除失败！");
                    }
                }
            })
        }
    }

    //修改
    function Update(openFareId) {
        localStorage["openFareId"] = openFareId;
        location.href = '/OpenFare/UpdateOpenFare/' + openFareId;
    }
        //修改
    function XiangQing(openFareId) {
        localStorage["openFareId"] = openFareId;
        location.href = '/OpenFare/DetailsOpenFare/' + openFareId;
    }
</script>